package ru.compscicenter.uno.wizard;

import ru.compscicenter.uno.GameSettings;
import ru.compscicenter.uno.model.Model;
import ru.compscicenter.uno.view.SwingViewer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: SLeeP
 * Date: 07.03.12
 * Time: 4:17
 */

public class Panel3ClientRulesDescriptor extends WizardPanelDescriptor  {
    public static final String IDENTIFIER = "CLIENT_RULES";
    private String next;
    private Panel3ClientRules panel2ClientRules;
    private GameSettings gameSettings;

    public Panel3ClientRulesDescriptor(GameSettings gameSettings) {
        this.gameSettings = gameSettings;
        panel2ClientRules = new Panel3ClientRules();
        setPanelDescriptorIdentifier(IDENTIFIER);
        setPanelComponent(panel2ClientRules);
        checkStartGame();
    }

    public Object getNextPanelDescriptor() {
        return Panel4ClientWaitDescriptor.IDENTIFIER;
    }

    public Object getBackPanelDescriptor() {
        return Panel2ClientDescriptor.IDENTIFIER;
    }
    
    private void checkStartGame() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if (gameSettings.clientController != null && gameSettings.clientController.getModel() != null) {
                        gameSettings.view = new SwingViewer(gameSettings.clientController);
                        gameSettings.view.run();
                        getWizard().close(Wizard.FINISH_RETURN_CODE);
                        break;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }
}
